package cn.kgc.controller;

import cn.kgc.pojo.BackendUser;
import cn.kgc.service.ManSercive;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;


@Controller
@RequestMapping("/man")
public class ManController {
    @Resource
    private ManSercive manSercive;

    @RequestMapping("/magLogin")
    public String jump() {
        return "backendlogin";
    }

    @RequestMapping(value = "/dologin", method = RequestMethod.POST)
    public String magLogin(HttpSession session, @RequestParam String userCode, @RequestParam String userPassword) throws Exception {
        BackendUser backendUser = null;
        backendUser = manSercive.magLogin(userCode, userPassword);
        //登录成功
        if (backendUser.getUserName() != null && backendUser.getUserName().length() > 0) {
            session.setAttribute("userSession", backendUser);
            return "backend/main";
        } else{
            //登录失败，重新登陆
            session.setAttribute("loginMessage", "用户名或者密码不正确");
            return "backendlogin";
        }

    }

    /**
     * 注销
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpSession session) {
        //移除session中userName
        session.removeAttribute("userSession");
        //销毁session对象
        session.invalidate();
        return "backendlogin";
    }


}